package com.climber.android.lib_share

import android.content.Context
import com.climber.android.lib_share.entity.ShareConfig
import com.climber.android.lib_share.platform.qq.QQLogin
import com.climber.android.lib_share.platform.qq.QQShare
import com.climber.android.lib_share.platform.qq.QQZoneShare
import com.climber.android.lib_share.platform.sina.WeiboLogin
import com.climber.android.lib_share.platform.sina.WeiboShare
import com.climber.android.lib_share.platform.wechat.WeChatFavouriteShare
import com.climber.android.lib_share.platform.wechat.WeChatLogin
import com.climber.android.lib_share.platform.wechat.WeChatMomentsShare
import com.climber.android.lib_share.platform.wechat.WeChatShare
import com.climber.android.lib_share.sensor.EmptyAuthorizeAction
import com.climber.android.lib_share.sensor.AuthorizeAction
import com.climber.android.lib_share.sensor.ShareAction
import com.climber.android.lib_share.sensor.ShareResultCallBack
import com.mob.MobSDK

/**
 * Created by Mikes at 2020-03-19 16:00
 */
enum class Platform{
    QQ,
    QZone,
    Wechat,
    WechatFavourite,
    WechatMoments,
    SinaWeibo;
}

class OneShare internal constructor(context: Context){
    private var shareBuilder : (ShareResultCallBack.Builder.() -> Unit) ?=null
    private var shareConfig : ShareConfig?=null

    init {
        MobSDK.init(context, BuildConfig.MOB_APP_KEY, BuildConfig.MOB_APP_SECRET)
        submitPolicyGrantResult()
    }

    companion object{
        internal const val TAG = "OneShare"

        // 单例模式
        @Volatile
        private var instance: OneShare? = null
        fun getInstance(context: Context) = instance ?: synchronized(this) {
            instance ?: OneShare(context).also { instance = it }
        }
    }

    /**
     * 回传用户隐私授权结果
     *
     *
     */
    private fun submitPolicyGrantResult(){
        MobSDK.submitPolicyGrantResult(true, null)
    }

    fun setShareBuilder(builder : ShareResultCallBack.Builder.() -> Unit) : OneShare{
        this.shareBuilder = builder

        return this
    }

    fun setShareConfig(shareConfig : ShareConfig) : OneShare{
        this.shareConfig = shareConfig

        return this
    }

    /**
     * 获取具体平台的操作实例
     */
    fun getShareAction(platform : Platform) : ShareAction {
        val builder = this.shareBuilder
                ?: throw RuntimeException("share builder must be initialed at first.")

        val config = this.shareConfig
                ?: throw RuntimeException("share config must be initialed at first.")

        return when(platform){
            Platform.QQ -> QQShare(config, builder)
            Platform.QZone -> QQZoneShare(config, builder)
            Platform.Wechat -> WeChatShare(config, builder)
            Platform.WechatFavourite -> WeChatFavouriteShare(config, builder)
            Platform.WechatMoments -> WeChatMomentsShare(config, builder)
            Platform.SinaWeibo -> WeiboShare(config, builder)
        }
    }

    fun getAuthorizeAction(platform : Platform) : AuthorizeAction {
        return when(platform){
            Platform.QQ -> QQLogin()
            Platform.Wechat -> WeChatLogin()
            Platform.SinaWeibo -> WeiboLogin()
            else -> EmptyAuthorizeAction()
        }
    }
}